﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PN264.Controls
{
    /// <summary>
    /// Provides a control that allows the user to view workers in the worker pool.
    /// </summary>
    public partial class WorkerView : UserControl
    {
        #region Constructors
        public WorkerView()
        {
            InitializeComponent();

            this.workerPoolCapacityInput.Items.AddRange(new object[] { 1, 2, 3, 4, "(CPU Count)" });

            this.workerPoolWorkerAddedCallback = new EventHandler<WorkerEventArgs>(this.WorkerPool_WorkerAdded);
            this.workerPoolWorkerRemovedCallback = new EventHandler<WorkerEventArgs>(this.WorkerPool_WorkerRemoved);
            this.workerStatusChangedCallback = new EventHandler<WorkerStatusChangedEventArgs>(this.Worker_StatusChanged);
            WorkerPool.WorkerAdded += this.workerPoolWorkerAddedCallback;
            WorkerPool.WorkerRemoved += this.workerPoolWorkerRemovedCallback;
        }
        #endregion
        #region Fields
        private readonly EventHandler<WorkerEventArgs> workerPoolWorkerAddedCallback;
        private readonly EventHandler<WorkerEventArgs> workerPoolWorkerRemovedCallback;
        private readonly EventHandler<WorkerStatusChangedEventArgs> workerStatusChangedCallback;
        #endregion
        #region Methods
        void WorkerPool_WorkerRemoved(object sender, WorkerEventArgs e)
        {
            e.Worker.StatusChanged -= this.workerStatusChangedCallback;
        }

        void Worker_StatusChanged(object sender, WorkerStatusChangedEventArgs e)
        {
            
        }
        void WorkerPool_WorkerAdded(object sender, WorkerEventArgs e)
        {
            e.Worker.StatusChanged += this.workerStatusChangedCallback;
        }
        #endregion

        private void workerPoolCapacityInput_SelectedIndexChanged(object sender, EventArgs e)
        {
            int newCapacity = 0;

            if (this.workerPoolCapacityInput.SelectedItem.ToString() == "(CPU Count)")
            {
                newCapacity = Environment.ProcessorCount;
            }
            else
            {
                newCapacity = int.Parse(this.workerPoolCapacityInput.SelectedItem.ToString());
            }

            WorkerPool.Capacity = newCapacity;
        }
    }
}
